/**
  ******************************************************************************
  * @file    GPIO_Toggle\main.c
  * @author  MCD Application Team
  * @version V2.0.4
  * @date    26-April-2018
  * @brief   This file contains the main function for GPIO Toggle example.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm8s_conf.h"
#include "adc1.h"
#include "timer.h"
#include "pwm.h"
#include "i2c_slave.h"
#include "stdio.h"
/**
  * @addtogroup GPIO_Toggle
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Evalboard I/Os configuration */




#define OPTION_BYTES_ADDR  0x4803


__IO uint32_t ch1_100us_cnt = 0;
__IO uint32_t ch2_100us_cnt = 0;
//__IO uint32_t ch3_100us_cnt = 0;
//__IO uint32_t ch4_100us_cnt = 0;




/* Private function prototypes -----------------------------------------------*/
//void Delay(__IO uint32_t nTime);


//static int test = 0x1234;
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/


/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar (char c)
#define GETCHAR_PROTOTYPE int getchar (void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar (char c)
#define GETCHAR_PROTOTYPE char getchar (void)
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar (int c)
#define GETCHAR_PROTOTYPE int getchar (void)
#endif /* _RAISONANCE_ */


void Set_OptionByte(void)
{
	u16 Option_Bytes;
	Option_Bytes = FLASH_ReadOptionByte(OPTION_BYTES_ADDR);
	if(Option_Bytes & 0x01 == 0)
	{
		FLASH_ProgramOptionByte(OPTION_BYTES_ADDR, (u8)Option_Bytes | 0x01);
	}
}


/**
  * @brief  Configure system clock to run at Maximum clock speed and output the 
  *         system clock on CCO pin
  * @param  None
  * @retval None
  */
static void CLK_Config(void)
{
    CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
}


#define LED_GPIO_PORT	GPIOB
#define LED_GPIO_PINS	GPIO_PIN_5
void gpio_config(){
// PC6 PC5 PD2 PC7 PD3
	GPIOC->ODR = 0x28;
	GPIOD->ODR |= 0x04;

	//GPIO_WriteLow(GPIOD, GPIO_PIN_4);

#if 0
	/* Initialize I/Os in Output Mode */
	GPIO_Init(GPIOB, (GPIO_Pin_TypeDef)GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST);
	GPIO_WriteLow(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS);

	GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)GPIO_PIN_1, GPIO_MODE_OUT_PP_HIGH_FAST);
	GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)GPIO_PIN_2, GPIO_MODE_OUT_PP_HIGH_FAST);
	GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)GPIO_PIN_3, GPIO_MODE_OUT_PP_HIGH_FAST);
	GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);
	GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
#else

	GPIO_Init(GPIOB, (GPIO_Pin_TypeDef)GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);// R
	GPIO_Init(GPIOB, (GPIO_Pin_TypeDef)GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);// g

	
	GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)GPIO_PIN_2, GPIO_MODE_OUT_PP_HIGH_FAST);
	GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)GPIO_PIN_3, GPIO_MODE_OUT_PP_HIGH_FAST);

	GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_3, GPIO_MODE_OUT_PP_HIGH_FAST);
	GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
	GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_6, GPIO_MODE_OUT_PP_HIGH_FAST);
	GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_7, GPIO_MODE_OUT_PP_HIGH_FAST);

#endif
	GPIO_WriteLow(GPIOB, GPIO_PIN_4); 	// LEDR ON
	GPIO_WriteHigh(GPIOB, GPIO_PIN_5); 	// LEDG OFF 

	GPIOC->ODR = 0x3c;
	GPIOD->ODR |= 0x04;

	//GPIOD->ODR  = 0;
	//GPIOC->ODR  = 0;

}

#define DEBUG	0
static void hardware_init(void){
	//TIM1_Config();
#if DEBUG
	 pwm_gpio_deinit();
#else
	// pwm_init(); 
	// pwm_chanel_config(PWM_CH0, 5, 99, 1);
	// pwm_chanel_config(PWM_CH1, 5, 99, 1);
	 // pwm_chanel_config(PWM_CH2, 1, 10, 1);	
	gpio_config();
	TIM1_Config();
	 //tim2_pwm_config();
	 //TIM4_Config();
#endif	 
	// gpio_config();
	 //i2c_slave_config();
	 //ADC_Config();
	 //tim2_pwm_output();
}


/**
  * @brief Retargets the C library printf function to the UART.
  * @param c Character to send
  * @retval char Character sent
  */
PUTCHAR_PROTOTYPE
{
  /* Write a character to the UART1 */
  UART1_SendData8(c);
  /* Loop until the end of transmission */
  while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);

  return (c);
}


#define DELAY_US(cnt) for (int i = 0; i<cnt;i++);

#define NUM 12
uint16_t send_Buf[NUM+1] = {0};

void output_pwm(uint8_t ch){
	int i = 0;
	int dir = 0;
    int duty = 0;

	//GPIOA->ODR = 0x04;
	if (ch == 0){
		while(1){
			// if (i == 0)GPIOA->ODR = 0x04;
			TIM1->CCR1L = send_Buf[i];			
			i++;
			if (i == NUM){
				break;
			}			
		}
	}

//	GPIOA->ODR = 0x2c;

}


/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{ 
	//Set_OptionByte();
	CLK_Config();
 	hardware_init();

	TIM1_SetCompare1(0);
#if 0
	UART1_DeInit();
	UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
	          UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
#endif	
	int i = 0;
	//int ch_idx = 0;
	send_Buf[NUM] = 0;
	for (i = 0; i < 6; i++)
	{
		send_Buf[i] = (i+1)*2;
	}
	for (i = 0; i < 6; i++)
	{
		send_Buf[6+i] = 12 - (i+1)*2;//72 - (i+1)*2;
	}

	GPIOC->ODR &= ~(0x08);
	GPIOD->ODR &= ~0x04;
	// PC7:0, PC5:1, PD2:0,
	while (1)
	{
		//if (i == 0) GPIOC->ODR &= ~(0x08);
		//if (i == 14) GPIOC->ODR |= 0x08;
		TIM1->CCR1L = send_Buf[i];			
		i++;
		if (i == NUM){ 
			GPIOC->ODR |= 0x08;
			DELAY_US(160);// 170
			i = 0;
			GPIOC->ODR &= ~(0x08);
		}
		//DELAY_US(1);
		//output_pwm(0);
	}

}


#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
